package com.itheima.service.impl;

import com.itheima.mapper.DeptMapper;
import com.itheima.mapper.EmpMapper;
import com.itheima.pojo.Dept;
import com.itheima.service.DeptService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.data.jdbc.JdbcRepositoriesAutoConfiguration;
import org.springframework.jdbc.support.JdbcTransactionManager;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;
@Slf4j
@Service
public class DeptServiceImpl implements DeptService {
    @Autowired
    private DeptMapper deptMapper;

    @Autowired
    private EmpMapper empMapper;

    @Override
    public List<Dept> list() {
        List<Dept> list=deptMapper.list();
        return list;
    }

    /**
     * 删除部门时,不仅要删部门,还要把该部门下的员工数据页删了
     * @param id
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteById(Integer id) {
        //根据部门id删除部门信息
        deptMapper.deleteById(id);


        //模拟 异常发生
        int i=1/0;


        //删除部门下所有员工信息
        empMapper.deleteByDeptId(id);

    }

    @Override
    public void add(Dept dept) {
        //补全参数
        dept.setCreateTime(LocalDateTime.now());
        dept.setUpdateTime(LocalDateTime.now());
        deptMapper.insert(dept);
    }

    /**
     * 根据id查询部门信息
     *
     * @param id
     * @return
     */
    @Override
    public Dept getById(Integer id) {
        Dept dept=deptMapper.getById(id);
        return dept;
    }

    /**
     * 修改部门信息
     *
     * @param dept
     */
    @Override
    public void update(Dept dept) {
        //补全参数 update_time
        dept.setUpdateTime(LocalDateTime.now());

        deptMapper.update(dept);
    }
}
